package com.taoyibao.hongcanting.ui.mine.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.ui.mine.delegate.ChangeNickDelegate;
import com.taoyibao.hongcanting.utils.ToastUtils;

/**
 * @author TangAnna
 * @description: 修改昵称
 * @date :${DATA} 13:52
 */
public class ChangeNickActivity extends BaseActivityPresenter<ChangeNickDelegate> implements View.OnClickListener {
    @Override
    protected Class<ChangeNickDelegate> getDelegateClass() {
        return ChangeNickDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.get(R.id.btn_changeNick_confirm).setOnClickListener(this);
    }

    public void changeNick() {
        JsonApiWrapper.getApiService().userNickname(SpUtils.getToken(),SpUtils.getMemberId(), viewDelegate.getInputNick())
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel companyBaseModel) {
                        if (companyBaseModel.code == NetCode.HTTP_200) {
                            setResult();
                        } else if (!TextUtils.isEmpty(companyBaseModel.msg)) {
                            ToastUtils.showToast(companyBaseModel.msg);
                        } else {
                            super.onNext(companyBaseModel);
                        }
                    }
                });
    }

    /**
     * 修改成功之后返回数据
     */
    private void setResult() {
        Intent intent = new Intent();
        intent.putExtra(CodeConstant.CHANGE_NICKNAME, viewDelegate.getInputNick());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_changeNick_confirm://确认
                if (!TextUtils.isEmpty(viewDelegate.getInputNick())) {
                    changeNick();
                }
                break;
        }
    }
}
